package com.dave.stream;

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Random;
import java.util.UUID;
import java.util.stream.Stream;

/**
 * @Author Dave
 * @Date 2018/8/10
 * @Description 创建流
 */
public class CreateStream {

    public static void main(String[] args) {
        //创建流
        Stream<String> stream = Stream.of("有天", "我睡醒", "看到", "我的身边没有你");
        int sum = stream.mapToInt(String::length).sum();

        //空流
        Stream<Object> empty = Stream.empty();

        //由文件生成流
        try (Stream<String> lines = Files.lines(Paths.get("/Users/mac/Downloads/data.txt"), Charset.defaultCharset())) {
            long count = lines.flatMap(e -> Arrays.stream(e.split(" ")))
                    .distinct()
                    .count();
            System.out.println("单词数" + count);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //函数生成流 类似python
        Stream.iterate(0, n -> n + 2)
                .limit(10)
                .mapToInt(e -> e)
                .sum();

        //斐波那契数列
        Stream.iterate(new int[]{0, 1},
                t -> new int[]{t[1], t[0] + t[1]})
                .limit(20)
                .forEach(t -> System.out.println("(" + t[0] + "," + t[1] + ")"));


        //generate 生成流
        Stream.generate(UUID::randomUUID)
                .limit(5)
                .map(e -> e.toString().split("-"))
                .flatMap(Arrays::stream)
                .forEach(System.out::println);
    }
}
